package com.gxa.jobs;


import com.gxa.endpoint.WebSocketEndPoint;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.websocket.Session;
import java.util.Date;
import java.util.Map;

@Component
@EnableScheduling
@Slf4j
public class ScheduleTask {
//      每60秒 执行一次
//     2022-08-18 14:29:00
//     2022-08-18 14:30:00
//     2022-08-18 14:31:00
//     2022-08-18 14:32:00
//     2022-08-18 14:33:00
    @Scheduled(cron = "0/60 * *  * * ?")

//      每10秒 执行一次
//     2022-08-18 14:29:00
//     2022-08-18 14:29:10
//     2022-08-18 14:29:20
//     2022-08-18 14:29:30
//     2022-08-18 14:29:40
//    @Scheduled(cron = "0/10 * *  * * ?")
    public void sendMessage() {
        String message = "定时报时间,现在:" + new Date();
        log.info("{}", "**********定时任务执行***********");
        Map<String, Session> map = WebSocketEndPoint.getSessionMap();
        log.info("当前在线用户人数 {} 个！",map.size());
        for (String userId : map.keySet()) {
            map.get(userId).getAsyncRemote().sendText(message);
        }
    }
}

